package com.iwords.api.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.iwords.constants.Constants;
import com.iwords.service.WordFreDetailServiceLocal;
import com.iwords.service.WordService;
import com.iwords.service.object.SysLibWordsEntity;
import com.iwords.service.object.WordFrequencyDetailEntity;
import com.iwords.utils.CodeUtils;
import com.iwords.utils.StringUtils;
import com.iwords.api.web.base.BaseAction;

/**
 * @author       BOLIN.HBC 
 * @time         2011-4-5    
 * @description  高级词汇-SERVLET       
 */
@WebServlet(name = "advancedWordAction", urlPatterns = { "/advancedWord" })
public class AdvancedWordAction extends BaseAction<SysLibWordsEntity>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	private  WordService wordService;
	@EJB
	private WordFreDetailServiceLocal wordFreDetailService;

	public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {
		String action = req.getParameter("action");
		if (Constants.WORDS_LIST_ACTION.equals(action)) {
			doGetListByLibId(req, res);
		} else if (Constants.WORDS_SINGLE_ACTION.equals(action)) {
			doFindWordByWordId(req, res);
		}

	}

	public void doGetListByLibId(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {
		SysLibWordsEntity entity = initEntity(req);

		List<SysLibWordsEntity> resultList = wordService.findWordsBySysLibId(entity.getLibid());

		List<String> ids = new ArrayList<String>();
		for (SysLibWordsEntity word : resultList) {
			ids.add(word.getWordid() + "");
		}

		List<WordFrequencyDetailEntity> wordFreList = wordFreDetailService.getWordFreDetailListByIds(ids);

		Map<Long, List<WordFrequencyDetailEntity>> map = new HashMap<Long, List<WordFrequencyDetailEntity>>();

		for (final WordFrequencyDetailEntity wordFre : wordFreList) {
			if (map.containsKey(wordFre.getWordid())) {
				map.get(wordFre.getWordid()).add(wordFre);
			} else {
				map.put(wordFre.getWordid(), new ArrayList<WordFrequencyDetailEntity>() {
					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					{

						add(wordFre);

					}
				});
			}
		}

		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		com.iwords.utils.JSONObject defaultValue = new com.iwords.utils.JSONObject();

		defaultValue.putColumn("wordid", "syslibid", "meaning", "phonetics", "spelling", "createtime", "updatetime", "r", "l", "c", "t");
		for (SysLibWordsEntity word : resultList) {

			List<WordFrequencyDetailEntity> resultL = map.get(word.getWordid());
			int r = 0;
			int l = 0;
			int c = 0;
			int t = 0;

			for (WordFrequencyDetailEntity wordFre : resultL) {
				if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_1)) {
					r = Integer.parseInt(wordFre.getFrequency() + "");
				} else if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_2)) {
					l = Integer.parseInt(wordFre.getFrequency() + "");
				} else if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_3)) {
					c = Integer.parseInt(wordFre.getFrequency() + "");
				} else if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_4)) {
					t = Integer.parseInt(wordFre.getFrequency() + "");
				}

			}
			defaultValue.putValue(word.getWordid(), word.getLibid(), CodeUtils.encode(word.getMeaning()), CodeUtils.encode(word.getPhonetics()),
					CodeUtils.encode(word.getSpelling()), word.getCreatetime().getTime(), word.getUpdatetime().getTime(), r, l, c, t);

		}
		result.put(Constants.DEFAULT_VALUE, defaultValue);

		jsonPrint(res, result);
	}

	public void doFindWordByWordId(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {

		SysLibWordsEntity entity = initEntity(req);

		SysLibWordsEntity resultEntity = wordService.findWordByWordId(entity.getWordid());
		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		if (resultEntity != null) {

			JSONObject defaultValue = new JSONObject();

			defaultValue.put("wordid", resultEntity.getWordid());
			defaultValue.put("syslibid", resultEntity.getLibid());
			defaultValue.put("meaning", CodeUtils.encode(resultEntity.getMeaning()));
			defaultValue.put("phonetics", resultEntity.getPhonetics());
			defaultValue.put("spelling", resultEntity.getSpelling());
			defaultValue.put("createtime", resultEntity.getCreatetime().getTime());
			defaultValue.put("updatetime", resultEntity.getUpdatetime().getTime());
			List<WordFrequencyDetailEntity> wordFreList = wordFreDetailService.getWordFreDetailByWordId(entity.getWordid());
			int r = 0;
			int l = 0;
			int c = 0;
			int t = 0;

			for (WordFrequencyDetailEntity wordFre : wordFreList) {
				if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_1)) {
					r = Integer.parseInt(wordFre.getFrequency() + "");
				} else if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_2)) {
					l = Integer.parseInt(wordFre.getFrequency() + "");
				} else if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_3)) {
					c = Integer.parseInt(wordFre.getFrequency() + "");
				} else if (wordFre.getItemtype().equals(Constants.ITEM_TYPE_4)) {
					t = Integer.parseInt(wordFre.getFrequency() + "");
				}

			}

			defaultValue.put("r", r);
			defaultValue.put("l", l);
			defaultValue.put("c", c);
			defaultValue.put("t", t);
			result.put("defaultValue", defaultValue);
		} else {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, CodeUtils.encode("该单词不存在"));

		}

		jsonPrint(res, result);

	}

	public SysLibWordsEntity initEntity(HttpServletRequest req) {

		SysLibWordsEntity entity = new SysLibWordsEntity();
		String wordid = req.getParameter("wordid");
		if (StringUtils.isNotBlank(wordid)) {
			entity.setWordid(Long.parseLong(req.getParameter("wordid")));
		}
		String libid = req.getParameter("libid");
		if (StringUtils.isNotBlank(libid)) {
			entity.setLibid(Long.parseLong(req.getParameter("libid")));
		}

		return entity;
	}

}